//
//  ZBSwitchView.m
//  YouHui
//
//  Created by  on 11-12-6.
//  Copyright (c) 2011年 netease. All rights reserved.
//

#import "ZBSwitchView.h"

@interface ZBSwitchView()
-(void)tapAction:(UITapGestureRecognizer*)sender;
@end

@implementation ZBSwitchView

@synthesize switchOnImage=_switchOnImage;
@synthesize switchOffImage=_switchOffImage;
@synthesize on=_on;

-(id)initWithFrame:(CGRect)frame {
    self =[super initWithFrame:frame];
    if (self) {
        _imageView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];
        [self addSubview:_imageView];
        
        UITapGestureRecognizer *tap =[[[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                             action:@selector(tapAction:)] autorelease];
        [self addGestureRecognizer:tap];
        self.userInteractionEnabled = YES;
    }
    return self;
}

-(void)dealloc {
    [_switchOffImage release];
    [_switchOnImage release];
    [super dealloc];
}

-(void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    _imageView.frame = self.bounds;
}

-(void)setSwitchOnImage:(UIImage *)switchOnImage {
    [switchOnImage retain];
    [_switchOnImage release];
    _switchOnImage = switchOnImage;
    
    if (self.on) {
        _imageView.image = _switchOnImage;
    }
}

-(void)setSwitchOffImage:(UIImage *)switchOffImage {
    [switchOffImage retain];
    [_switchOffImage release];
    _switchOffImage = switchOffImage;
    
    if (!self.on) {
        _imageView.image = _switchOffImage;
    }
}

-(void)setOn:(BOOL)on {
    if (_on!=on) {
        _on = on;
        _imageView.image = _on?_switchOnImage:_switchOffImage;
    }
}

-(void)tapAction:(UITapGestureRecognizer*)sender {
    if (sender.state==UIGestureRecognizerStateRecognized) {
        self.on = !self.on;
        [super sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

@end
